package com.zls.controller;

import com.zls.mapper.TTaskMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.page.Result;
import com.zls.pojo.TProducter;
import com.zls.pojo.TTask;
import com.zls.service.TProducerService;
import com.zls.service.TTaskService;
import lombok.AllArgsConstructor;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("tproducer")
@RestController
public class TProducerController {
    @Autowired
    private TProducerService tProducerService;
    @GetMapping("findAll")
    public List<TProducter> findAll(){
        List<TProducter> all = tProducerService.list();
        return all;
    }
    /**
     * 用户列表分页查询
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult page = tProducerService.findPage(queryPageBean);
        return page;
    }

    /**
     * 用户添加
     * @param tProducter
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody TProducter tProducter){
        int i = tProducerService.addTpro(tProducter);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据id查
     * @param id
     * @return
     */
    @GetMapping("findById")
    public Result findById(@RequestParam("id") int id){
        TProducter tProducter = tProducerService.selectById(id);
        Result result=new Result();
        if (tProducter!=null){
            result.setData(tProducter);
            result.setFlag(true);
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }

    /**
     * 修改
     * @param tProducter
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody TProducter tProducter){
        boolean i = tProducerService.updateById(tProducter);
        if (i){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 登录
     * @param tProducter
     * @return
     */
    @PostMapping("login")
    public String login( TProducter tProducter, HttpSession httpSession){
        TProducter i = tProducerService.login(tProducter);
        httpSession.setAttribute("producter",i);
        if (i!=null){

            return "yes";
        }else {
            return "no";
        }
    }
    @Autowired
    private TTaskMapper tTaskMapper;

    @PostMapping("productindex")
    public Result productindex(HttpSession httpSession){
        TProducter producer = (TProducter) httpSession.getAttribute("producter");
        List<TTask> tTasks = tTaskMapper.selectByProId(producer.getId());
        Result result=new Result();
        if (producer!=null){
            result.setData(tTasks);
            result.setData2(producer);
            result.setFlag(true);
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }
    @Autowired
    private TTaskService tTaskService;
    @PostMapping("submit")
    public Result submit(@RequestBody TTask tTask){
        int i=tTaskService.submit(tTask);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("id") int id){
        boolean i = tProducerService.removeById(id);
        if (i){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
